//
//  HOTCHATPopVIew.m
//  ChatForHot
//
//  Created by wr on 2018/10/19.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "HOTCHATPopVIew.h"

@interface HOTCHATPopVIew ()<CAAnimationDelegate>

@property (nonatomic,strong) UIView *alertview;
@property (nonatomic,strong) UILabel *titleLabel;

@property (nonatomic,strong) UITextView *policyTextView;
@property (nonatomic,strong) UIButton *cancelbutton;
@property (nonatomic,strong) UIButton *agreebutton;
@end

@implementation HOTCHATPopVIew

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (void)HOTCHATdidInitialized {
    self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.0];
    
    self.alertview = [[UIView alloc] init];
    self.alertview.backgroundColor = [UIColor whiteColor];
    self.alertview.layer.cornerRadius = 5;
    self.alertview.layer.masksToBounds = YES;
    [self addSubview:self.alertview];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.titleLabel.text = @"Tip";
    self.titleLabel.textColor = [UIColor colorFromHexString:@"111111"];
    [self.alertview addSubview:self.titleLabel];
    
    self.policyTextView = [[UITextView alloc] init];
    self.policyTextView.editable = NO;
    self.policyTextView.selectable = NO;
    self.policyTextView.scrollEnabled = YES;
    [self.alertview addSubview:self.policyTextView];
    
    self.cancelbutton = [[UIButton alloc] init];
    [self.cancelbutton setTitle:NSLocalizedString(@"CANCELBUTTON", nil) forState:UIControlStateNormal];
    [self.cancelbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.alertview addSubview:self.cancelbutton];
    
    @weakify(self)
    [[self.cancelbutton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self hideView];
    }];
    
    
    self.agreebutton = [[UIButton alloc] init];
    [self.agreebutton setTitle:NSLocalizedString(@"OKBUTTON", nil) forState:UIControlStateNormal];
    [self.agreebutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.agreebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self.alertview addSubview:self.agreebutton];
    
    [[self.agreebutton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATagreeButtonDidTouch)]) {
            [self.delegate HOTCHATagreeButtonDidTouch];
        }
    }];
    
    [self HOTCHATLoadTextFile];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.alertview.size = CGSizeMake(kScreenWidth - 40, kScreenHeight - 300);
    self.alertview.top = 150;
    self.alertview.left = 20;
    
    self.titleLabel.size = CGSizeMake(100, 40);
    self.titleLabel.top = 5;
    self.titleLabel.centerX = self.alertview.centerX;
    
    self.policyTextView.size = CGSizeMake(kScreenWidth - 40 - 30, self.alertview.height - 120);
    self.policyTextView.top = self.titleLabel.bottom + 15;
    self.policyTextView.left = 15;
    
    self.cancelbutton.size = CGSizeMake((kScreenWidth - 40) / 2, 50);
    self.cancelbutton.left = 0;
    self.cancelbutton.top = self.policyTextView.bottom + 10;
    
    self.agreebutton.size = CGSizeMake((kScreenWidth - 40) / 2, 50);
    self.agreebutton.left = (kScreenWidth - 40) / 2;
    self.agreebutton.top = self.policyTextView.bottom + 10;
}

- (void)HOTCHATLoadTextFile {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Policy-English" ofType:@"txt"];
    NSError *error = nil;
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (str.length > 0) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 5.0;
        NSDictionary *attrsDictionary1 = @{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                           NSParagraphStyleAttributeName:paragraphStyle,
                                           NSForegroundColorAttributeName:HOTCHATrgb(26, 26, 26)};
        [attributedString addAttributes:attrsDictionary1 range:NSMakeRange(0, str.length)];
        NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle2.lineSpacing = 3;
        self.policyTextView.attributedText = attributedString;
    }
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [self.layer addAnimation:animation forKey:nil];
}

- (void)hideView {
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    animation.delegate = self;
    
    [self.layer addAnimation:animation forKey:nil];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        UIView *superView = self;
        if (superView != nil) {
            [self removeFromSuperview];
        }
    }
}
@end
